package handler

import (
	"api-gate/global"
	__ "api-gate/proto"
	"api-gate/request"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"path/filepath"
)

func VideoAdd(c *gin.Context) {
	var req request.VideoAdd
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	add, err := global.VideoRpc.VideoAdd(c, &__.VideoAddRequest{
		Title:             req.Title,
		Author:            req.Author,
		Times:             req.Times,
		Likes:             int64(req.Likes),
		Types:             req.Types,
		Img:               req.Img,
		UploadMethod:      req.UploadMethod,
		MemoryAddress:     req.MemoryAddress,
		TranscodingScheme: req.TranscodingScheme,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": add,
	})
	return
}

func Upload(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)
	fi := filepath.Ext(file.Filename)
	//name := time.Now().Format("2006-01-02 15:04:05") + fi
	if file.Size >= 15*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "文件大小错误",
			"data": nil,
		})
		return
	}
	if fi != ".png" && fi != ".mp4" {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "文件类型错误",
			"data": nil,
		})
		return
	}
	dst := "./upload/img/" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)

	c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
}

func List(c *gin.Context) {
	var req request.List
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	list, err := global.VideoRpc.VideoList(c, &__.VideoListRequest{
		Page: req.Page,
		Size: req.Size,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": list,
	})
	return
}

func Login(c *gin.Context) {
	var req request.Login
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	login, err := global.VideoRpc.UserLogin(c, &__.UserLoginRequest{
		Img:     req.Img,
		VideoId: int64(req.VideoId),
		Phone:   req.Phone,
		Name:    req.Name,
		Text:    req.Text,
		Address: req.Address,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": login,
	})
	return
}

func Info(c *gin.Context) {
	var req request.Info
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	info, err := global.VideoRpc.VideoInfo(c, &__.UserInfoRequest{UserId: req.UserId})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": info,
	})
	return
}
